package worker

import (
	"cron-go/configs"
	"cron-go/internal/pkg/crontab/dao"
	"cron-go/internal/pkg/crontab/enum"
	"cron-go/internal/pkg/crontab/models"
)

func WorkByID(taskID int64) {
	info, _ := new(dao.TaskDao).FindOneByID(taskID)
	Work(info)
}

func Work(t *models.TaskMysqlModel) {
	workerType := t.WorkerType
	switch workerType {
	case enum.WorkerID0:
		NotifyWork(t, 0) //无需工作的任务
	case enum.WorkerIDHttp:
		println("http")
		HttpWork(t) //通过http通知工作
	case enum.WorkerIDShell:
		println("shell")
		ShellWork(t)
	default:
		println("other")
	}
}

func FinishWorker(t *models.TaskMysqlModel, workerStatus int) {
	//update task
	dao.TaskDaoServer.UpdateDoStatus([]int64{t.ID}, workerStatus)
	//add finish：干完活了
	m := new(models.TaskFinishMysqlModel)
	m.TaskID = t.ID
	m.Status = 0
	m.ServerName = configs.HOST
	m.WorkerStatus = workerStatus
	dao.FinishDaoServer.Create(m)
}
